package com.hui.dao.impl;

import com.hui.dao.CourseDao;
import com.hui.pojo.Course;
import com.hui.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import javax.management.Query;
import javax.xml.transform.Source;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/07/11/18:13
 * @Description:
 */
public class CourseDaoImpl implements CourseDao {
    @Override
    public List<Course> getCourseList() {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "SELECT id,course_name,price,sort_num,`status` FROM course WHERE is_del = ?";
        try {
            List<Course> courseList = qr.query(sql, new BeanListHandler<>(Course.class), 0);
            return courseList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public List<Course> getCourseByCourseNameAndStatus(String courseName, String status) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            // 封装条件
            ArrayList<Object> list = new ArrayList<>();
            list.add(0);
            // sql 语句
            StringBuffer sb = new StringBuffer("SELECT  id,course_name,price,sort_num,`status` FROM course WHERE is_del = ? ");
            // 如果有 courseName  条件
            if (courseName != null && courseName != "") {
                courseName = "%" + courseName + "%";
                sb.append(" AND course_name LIKE ? ");
                list.add(courseName);
            }
            // 如果有 status  条件
            if (status != null && status != "") {
                sb.append(" AND `status`= ? ");
                list.add(Integer.parseInt(status));
            }
            // 执行查询操作，并返回结果
            List<Course> courseList = qr.query(sb.toString(), new BeanListHandler<>(Course.class), list.toArray());
            return courseList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int saveCourseSaleInfo(Course course) {
        try {
            String sql = "INSERT INTO course \n" +
                    "(\n" +
                    "course_name,\n" +
                    "brief,\n" +
                    "teacher_name,\n" +
                    "teacher_info,\n" +
                    "preview_first_field,\n" +
                    "preview_second_field,\n" +
                    "discounts,\n" +
                    "price,\n" +
                    "price_tag,\n" +
                    "share_image_title,\n" +
                    "share_title,\n" +
                    "share_description,\n" +
                    "course_description,\n" +
                    "`status`,\n" +
                    "course_img_url,\n" +
                    "create_time,\n" +
                    "update_time\n" +
                    ") \n" +
                    "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            // 将条件保存到params集合中
            Object[] params = {
                    course.getCourse_name(), course.getBrief(), course.getTeacher_name(), course.getTeacher_info(),
                    course.getPreview_first_field(), course.getPreview_second_field(), course.getDiscounts(), course.getPrice(),
                    course.getPrice_tag(), course.getShare_image_title(), course.getShare_title(), course.getShare_description(),
                    course.getCourse_description(), course.getStatus(), course.getCourse_img_url(), course.getCreate_time(),
                    course.getUpdate_time()};
            // 执行语句，并将结果返回
            return qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateCourseSaleInfo(Course course) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "UPDATE course SET \n" +
                    "course_name = ?,\n" +
                    "brief = ?,\n" +
                    "teacher_name = ?,\n" +
                    "teacher_info = ?,\n" +
                    "preview_first_field = ?,\n" +
                    "preview_second_field = ?,\n" +
                    "discounts = ?,\n" +
                    "price = ?,\n" +
                    "price_tag = ?,\n" +
                    "share_image_title = ?,\n" +
                    "share_title = ?,\n" +
                    "share_description = ?,\n" +
                    "course_description = ?,\n" +
                    "course_img_url = ?,\n" +
                    "update_time = ?\n" +
                    "WHERE id = ? ";
            Object[] params = {
                    course.getCourse_name(), course.getBrief(), course.getTeacher_name(), course.getTeacher_info(),
                    course.getPreview_first_field(), course.getPreview_second_field(), course.getDiscounts(), course.getPrice(), course.getPrice_tag(),
                    course.getShare_image_title(), course.getShare_title(), course.getShare_description(), course.getCourse_description(),
                    course.getCourse_img_url(), course.getUpdate_time(), course.getId()
            };
            // 执行修改操作，并将结果返回
            return qr.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Course getCourseSaleInfoById(int id) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "SELECT \n" +
                    "id,\n" +
                    "course_name,\n" +
                    "brief,\n" +
                    "teacher_name,\n" +
                    "teacher_info,\n" +
                    "preview_first_field,\n" +
                    "preview_second_field,\n" +
                    "discounts,\n" +
                    "price,\n" +
                    "price_tag,\n" +
                    "course_img_url,\n" +
                    "share_image_title,\n" +
                    "share_title,\n" +
                    "share_description,\n" +
                    "course_description,\n" +
                    "STATUS\n" +
                    "FROM course WHERE id = ?;";
            // 执行查询操作，并将结果返回
            Course course = qr.query(sql, new BeanHandler<>(Course.class), id);
            return course;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateCourseStatus(Course course) {
        try {
            String sql = "UPDATE course SET `status`=? , update_time=? WHERE id=?";
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            // 执行操作，并将结果返回
            int i = qr.update(sql, course.getStatus(), course.getUpdate_time(), course.getId());
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
